package com.senseuni.da.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.elvishew.xlog.XLog
import com.senseuni.da.R
import com.senseuni.da.databinding.FragmentMyAlbumBinding
import com.senseuni.da.ui.dialog.ChoiceListDialog
import com.senseuni.da.ui.dialog.SimpleEditTextDialog
import com.senseuni.da.ui.dialog.SimpleListItem
import com.senseuni.da.utils.LanguageHelper
import com.senseuni.da.utils.MmkvUtil
import com.senseuni.da.utils.SpUtil
import com.senseuni.da.utils.showToast
import kotlinx.coroutines.*

class MyAlbumFragment : Fragment() {

    companion object {
        fun newInstance() = MyAlbumFragment()
    }

    private lateinit var viewModel: MyAlbumViewModel

    private lateinit var binding: FragmentMyAlbumBinding

    private val coroutineScope = CoroutineScope(Dispatchers.Main)

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentMyAlbumBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.sivAlbumName.setOnClickListener {

            val dialog = SimpleEditTextDialog(context)
            dialog.setHint(getString(R.string.album_name_input_title))
            dialog.setTitle(getString(R.string.album_name))
            val albumName = MmkvUtil.getInstance().getString("album_name")
            if (!albumName.isNullOrEmpty()) {
                dialog.setContent(albumName, true)
            }
            dialog.setOnResultListener { _dialog, result ->
                _dialog.dismiss()
                if (result.isNotEmpty()) {
                    binding.sivAlbumName.setSummary(result)
                    MmkvUtil.getInstance().put("album_name", result)
                }
            }
            dialog.create().show()
        }

        binding.sivAlbumLocation.setOnClickListener {
            val dialog = SimpleEditTextDialog(context)
            dialog.setHint(getString(R.string.album_location_input_title))
            dialog.setTitle(getString(R.string.album_location))
            val albumLocation = MmkvUtil.getInstance().getString("album_location")
            if (!albumLocation.isNullOrEmpty()) {
                dialog.setContent(albumLocation, true)
            }
            dialog.setOnResultListener { _dialog, result ->
                _dialog.dismiss()
                if (result.isNotEmpty()) {
                    binding.sivAlbumLocation.setSummary(result)
                    MmkvUtil.getInstance().put("album_location", result)
                }
            }
            dialog.create().show()
        }

        binding.sivLanguageSet.setOnClickListener {
            val language = SpUtil.getLanguage()
            XLog.d("current language:${language}")
            val list = arrayListOf<SimpleListItem>()
            var item = SimpleListItem("en_US", "English")
            list.add(item)
            item = SimpleListItem("in_ID", "Indonesia")
            list.add(item)
            item = SimpleListItem("zh_CN", "简体中文")
            list.add(item)

            var defaultChoiceIndex = 0
            for (i in 0 until list.size) {
                if (language == list[i].key) {
                    defaultChoiceIndex = i;
                    break
                }
            }
            XLog.d("default index=$defaultChoiceIndex")
            ChoiceListDialog(context, getString(R.string.language_setting), list, defaultChoiceIndex)
                .setOnResultListener { _, result ->
                    XLog.d("choice lang:${result.key}")
                    coroutineScope.launch {
                        withContext(Dispatchers.Default) {
                            delay(500L)
                        }
                        LanguageHelper.switchLanguage(result.key)
                        binding.sivLanguageSet.setSummary(LanguageHelper.getLanguageName(SpUtil.getLanguage()))
                    }
                }
                .create().show()
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(MyAlbumViewModel::class.java)

        val albumName = MmkvUtil.getInstance().getString("album_name")
        if (albumName.isNullOrEmpty()) {
            binding.sivAlbumName.setSummary(R.string.unsetting)
        } else {
            binding.sivAlbumName.setSummary(albumName)
        }

        val albumLocation = MmkvUtil.getInstance().getString("album_location")
        if (albumLocation.isNullOrEmpty()) {
            binding.sivAlbumLocation.setSummary(R.string.unsetting)
        } else {
            binding.sivAlbumLocation.setSummary(albumLocation)
        }

        //获取当前语言
        val language = SpUtil.getLanguage()
        if (language.isEmpty()) {
            binding.sivLanguageSet.setSummary(R.string.unsetting)
        } else {
            binding.sivLanguageSet.setSummary(LanguageHelper.getLanguageName(language))
        }
    }

}